package com.example.expandlistviewdemo;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.Button;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnGroupClickListener;

import com.example.expandlistviewdemo.helper.ChildData;
import com.example.expandlistviewdemo.helper.DataHolder;
import com.example.expandlistviewdemo.helper.GroupData;
import com.example.expandlistviewdemo.helper.ViewHolder;
import com.example.expandlistviewdemo.util.Utils;
import com.example.expandlistviewdemo.view.ChildView;
import com.example.expandlistviewdemo.view.GroupView;
public class MainActivity extends Activity implements 
	GroupView.OnGroupClickListener,
	ChildView.OnChildClickListener{

	private Button btn;
	private ExpandableListView listView;
	
	private DataHolder dataHolder = new DataHolder();
	private ViewHolder viewholder = new ViewHolder();
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		listView = (ExpandableListView)findViewById(R.id.listview_group_list);
		
		//加载数据
		List<GroupData>  contentData = getContentData();
		dataHolder.setContentData(contentData);
		listView.setAdapter(new ExpandableListAdapter(this));
		//首次加载全部展开
		for (int i = 0; contentData != null && i < contentData.size(); i++) {
			listView.expandGroup(i);
		}
		//去掉系统默认的箭头图标
		listView.setGroupIndicator(null);
		//点击Group不收缩
		listView.setOnGroupClickListener(new OnGroupClickListener() {
				@Override 
				public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) { 
				    return true;
				}
		});
		
		btn = (Button)findViewById(R.id.btn_select_all);
		btn.setOnClickListener(new OnClickListener(){
			@Override
			public void onClick(View v) {
				if(btn.getText()=="全选"){
					btn.setText("全不选");
					dataHolder.setAllGroupAndChildChecked();
					viewholder.setAllGroupAndChildChecked();
				}else{
					btn.setText("全选");
					dataHolder.setAllGroupAndChildUnChecked();
					viewholder.setAllGroupAndChildUnChecked();
				}
			}
		});
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public void onChildChecked(int groupPosition, int childPosition) {
		dataHolder.setChildChecked(groupPosition, childPosition);
		viewholder.setChildChecked(groupPosition, childPosition);
	}

	@Override
	public void onChildUnChecked(int groupPosition, int childPosition) {
		dataHolder.setChildUnChecked(groupPosition, childPosition);
		viewholder.setChildUnChecked(groupPosition, childPosition);
	}

	@Override
	public void onGroupChecked(int groupPosition) {
		dataHolder.setGroupChecked(groupPosition);
		viewholder.setGroupChecked(groupPosition);
	}

	@Override
	public void onGroupUnChecked(int groupPosition) {
		dataHolder.setGroupUnChecked(groupPosition);
		viewholder.setGroupUnChecked(groupPosition);
	}

	private class ExpandableListAdapter extends BaseExpandableListAdapter {
		
		private Activity activity;
		
		public ExpandableListAdapter(Activity activity) {
			this.activity = activity;
		}
		@Override
		public Object getGroup(int groupPosition) {
			return dataHolder.getGroupData(groupPosition);
		}
		@Override
		public int getGroupCount() {
			return dataHolder.getGroupCount();
		}
		@Override
		public long getGroupId(int groupPosition) {
			return groupPosition;
		}
		
		@Override
		public Object getChild(int groupPosition, int childPosition) {
			return dataHolder.getChildData(groupPosition, childPosition);
		}
		
		@Override
		public int getChildrenCount(int groupPosition) {
			return dataHolder.getChildCount(groupPosition);
		}
		
		@Override
		public long getChildId(int groupPosition, int childPosition) {
			return childPosition;
		}

		@Override
		public boolean hasStableIds() {
			return true;
		}
		@Override
		public boolean isChildSelectable(int groupPosition, int childPosition) {
			return true;
		}
		
		@Override
		public View getGroupView(final int groupPosition, boolean isExpanded,
				View convertView, ViewGroup parent) {
			Utils.log("getGroupView:["+groupPosition+"]");
			
			final GroupView groupView = new GroupView((MainActivity)activity, getBaseContext());
			groupView.setGroupPosition(groupPosition);
			
			GroupData groupData = (GroupData)getGroup(groupPosition);
			groupView.getSelectGroup().setChecked(groupData.isGroupSelected());
			groupView.getGroupName().setText(groupData.getGroupName());
			
			viewholder.setGroupView(groupPosition, groupView);
			
			groupView.setOnClickListener(new OnClickListener(){
				@Override
				public void onClick(View v) {
					if(dataHolder.isGroupSelected(groupPosition)){
						((MainActivity)activity).onGroupUnChecked(groupPosition);
					}else{
						((MainActivity)activity).onGroupChecked(groupPosition);
					}
				}
			});
			
			return groupView;
		}
		
		@Override
		public View getChildView(final int groupPosition, final int childPosition,
				boolean isLastChild, View convertView, ViewGroup parent) {
			
			Utils.log("getChildView:["+groupPosition+","+childPosition+"]");
			
			ImageHolder holder;
			if(convertView == null){
				holder = new ImageHolder();
				convertView = new ChildView((MainActivity)activity,getBaseContext());
				holder.childView = (ChildView)convertView;
				convertView.setTag(holder);
			}else{
				holder = (ImageHolder)convertView.getTag();
			}
			
			ChildView childView = holder.childView;
			childView.setGroupPosition(groupPosition);
			childView.setChildPosition(childPosition);
			
			ChildData childData = (ChildData)getChild(groupPosition, childPosition);
			childView.getSelectChild().setChecked(childData.isChildSelected());
			if(holder.drawable == null){
				Resources res = getResources();
				holder.drawable = res.getDrawable(childData.getChildImage());
			}
			childView.getChildImage().setImageDrawable(holder.drawable);
			childView.getChildName().setText(childData.getChildName());
			
			viewholder.setChildView(groupPosition, childPosition, childView);
			childView.setOnClickListener(new OnClickListener(){
				@Override
				public void onClick(View v) {
					if(dataHolder.isChildSelected(groupPosition, childPosition)){
						((MainActivity)activity).onChildUnChecked(groupPosition, childPosition);
					}else{
						((MainActivity)activity).onChildChecked(groupPosition, childPosition);
					}
				}
			});
			
			return childView;
		}
		class ImageHolder{
			Drawable drawable;
			ChildView childView;
		}
	}

	private List<GroupData> getContentData(){
		List<GroupData> groupDatas = new ArrayList<GroupData>();
		for(int i=1;i<=5;i++){
			GroupData groupData = new GroupData();
			groupData.setGroupName("group name "+ i);
			groupData.setGroupSelected(false);
			List<ChildData> items = new ArrayList<ChildData>();
			for(int j=1;j<=3;j++){
				ChildData childData = new ChildData();
				childData.setChildImage(R.drawable.ic_launcher);
				childData.setChildName("childName " + j);
				childData.setChildSelected(false);
				items.add(childData);
			}
			groupData.setItems(items);
			groupDatas.add(groupData);
		}
		return groupDatas;
	}
}
